/*
 * Copyright (C) 2008 ZXing authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.mylhyl.zxing.scanner.encode;

import ohos.agp.colors.RgbColor;
import ohos.agp.components.Image;
import ohos.agp.components.element.ShapeElement;
import ohos.ai.cv.common.ConnectionCallback;
import ohos.ai.cv.common.VisionManager;
import ohos.ai.cv.qrcode.IBarcodeDetector;
import ohos.app.Context;
import ohos.global.resource.NotExistException;
import ohos.global.resource.WrongTypeException;
import ohos.media.image.ImageSource;
import ohos.media.image.PixelMap;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

public final class QREncode {

    private Image mImage;
    private Context mContext;
    private String mCodeMsg;
    public int mBackgroundColor;

    public QREncode(Context context) {
        this.mContext = context;
    }

    /**
     * 设置二维码背景色
     */
    public QREncode setQrBackground(int background) {
        int color = 0;
        try {
            color = mContext.getResourceManager().getElement(background).getColor();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (NotExistException e) {
            e.printStackTrace();
        } catch (WrongTypeException e) {
            e.printStackTrace();
        }
        this.mBackgroundColor = color;
        return this;
    }

    public int build(Image image, String codeMsg) {
        this.mImage = image;
        this.mCodeMsg = codeMsg;
        //建立与能力引擎的连接
        return VisionManager.init(mContext, connectionCallback);
    }

    ConnectionCallback connectionCallback = new ConnectionCallback() {
        @Override
        public void onServiceConnect() {
            IBarcodeDetector barcodeDetector = VisionManager.getBarcodeDetector(mContext);
            //定义码生成图像的尺寸，并根据图像大小分配字节流数组空间
            final int SAMPLE_LENGTH = 152;
            byte[] byteArray = new byte[SAMPLE_LENGTH * SAMPLE_LENGTH * 4];
            int result2 = barcodeDetector.detect(mCodeMsg, byteArray, SAMPLE_LENGTH, SAMPLE_LENGTH);

            InputStream inputStream = new ByteArrayInputStream(byteArray);
            //利用鸿蒙api将流解码为图片源
            ImageSource imageSource = ImageSource.create(inputStream, null);
            //根据图片源创建位图
            PixelMap pixelMap = imageSource.createPixelmap(null);
            mImage.setPixelMap(pixelMap);
            //设置背景
            ShapeElement shapeElement = new ShapeElement();
            shapeElement.setRgbColor(RgbColor.fromArgbInt(mBackgroundColor));
            mImage.setBackground(shapeElement);
            //当码生成能力使用完毕后，调用IBarcodeDetector的release()方法，释放资源
            barcodeDetector.release();
            //调用VisionManager.destroy()方法，断开与能力引擎的连接
            VisionManager.destroy();
            try {
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onServiceDisconnect() {
        }
    };

}
